package com.guigu.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsDto;
import com.guigu.erp.dto.ManufactureDesignProcedureModuleDto;
import com.guigu.erp.mapper.ManufactureDesignProcedureModuleMapper;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ManufactureDesignProcedureModuleServiceImpl
        extends ServiceImpl<ManufactureDesignProcedureModuleMapper,ManufactureDesignProcedureModule>
        implements ManufactureDesignProcedureModuleService {

    @Autowired
    private DesignProcedureService designProcedureService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;

    @Override
    @Transactional
    public boolean addProcessAndModule(ManufactureDesignProcedureDetailsDto dto) {
        //取出产品编号 和 工序主键id
        String productId = dto.getProductId();
        Integer id = dto.getId();
        //根据产品编号查询工序设计单表
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",productId);
        //得到工序设计单
        ManufactureDesignProcedure designProcedure = manufactureDesignProcedureService.getOne(wrapper);
        //获取工序设计单主键id
        Integer pid = designProcedure.getId();
        //根据工序表主键id得到工序表信息
        DesignProcedure procedureDetails = designProcedureService.getById(id);

        //根据工序设计表的主键id 和工序表的工序编号查询对应的工序明细表信息
        QueryWrapper<ManufactureDesignProcedureDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",pid);
        queryWrapper.eq("PROCEDURE_ID",procedureDetails.getProcedureId());
        ManufactureDesignProcedureDetails detailsServiceOne = manufactureDesignProcedureDetailsService.getOne(queryWrapper);

        //根据产品编号得到物料设计表
        QueryWrapper<DesignModule> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("PRODUCT_ID",productId);
        DesignModule moduleServiceOne = designModuleService.getOne(detailsQueryWrapper);

        //根据物料设计单表的主键查询物料明细表
        QueryWrapper<DesignModuleDetails> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID",moduleServiceOne.getId());

        List<DesignModuleDetails> details = dto.getDetails();
        BigDecimal sum = new BigDecimal(0);
        for (int i=0;i<details.size();i++){
            ManufactureDesignProcedureModule module = new ManufactureDesignProcedureModule();
            //设置父id
            module.setParentId(detailsServiceOne.getId());
            //设置details_number
            module.setDetailsNumber(i+1);
            //设置PROCEDURE_ID
            module.setProductId(details.get(i).getProductId());
            //设置PROCEDURE_NAME
            module.setProductName(details.get(i).getProductName());
            //设置 AMOUNT
            module.setAmount(details.get(i).getAmount());
            //用途类型
            module.setType(details.get(i).getType());
            //单价
            module.setCostPrice(details.get(i).getCostPrice());
            //小计
            module.setSubtotal(details.get(i).getSubtotal());
            //得到物料明细表信息
            List<DesignModuleDetails> list = designModuleDetailsService.list(qw);
            //判断物料明细表里面的产品编号和details里面的产品编号是否相等
            for(int j=0;j<list.size();j++){
                if (list.get(j).getProductId().equals(details.get(i).getProductId())){
                    //修改物料明细的可用数量
                    list.get(j).setResidualAmount(list.get(j).getResidualAmount().subtract(details.get(i).getAmount()));
                    designModuleDetailsService.updateById(list.get(j));
                }
            }
            //设置物料总成本
            BigDecimal temp = details.get(i).getSubtotal();
            sum=sum.add(temp);
            manufactureDesignProcedureModuleService.save(module);
        }

        //修改工序明细的当前工序物料标志 DESIGN_MODULE_TAG D002-1: 已设计
        detailsServiceOne.setDesignModuleTag("D002-1");
        //修改工序明细里面的物料成本
        detailsServiceOne.setModuleSubtotal(sum);
        boolean flag = manufactureDesignProcedureDetailsService.updateById(detailsServiceOne);
        return flag;
    }

    @Override
    @Transactional
    public boolean removeOperationItemDetails(ManufactureDesignProcedureModuleDto dto) {
        System.out.println(dto);
        //取出产品编号
        String productId = dto.getProductId();

        //根据产品编号重新工序设计单
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",productId);

        ManufactureDesignProcedure designProcedure = manufactureDesignProcedureService.getOne(wrapper);

        //根据工序设计单id查询工序明细表
        QueryWrapper<ManufactureDesignProcedureDetails> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("PARENT_ID",designProcedure.getId());
        detailsQueryWrapper.eq("PROCEDURE_ID",dto.getProcedureId());
        ManufactureDesignProcedureDetails procedureDetails = manufactureDesignProcedureDetailsService.getOne(detailsQueryWrapper);


        //根据产品编号查询物料设计单
        QueryWrapper<DesignModule> designModuleQueryWrapper = new QueryWrapper<>();
        designModuleQueryWrapper.eq("PRODUCT_ID",productId);
        DesignModule designModule = designModuleService.getOne(designModuleQueryWrapper);

        //根据物料设计单id查询物料明细信息
        QueryWrapper<DesignModuleDetails> serviceQueryWrapper = new QueryWrapper<>();
        serviceQueryWrapper.eq("PARENT_ID",designModule.getId());
        List<DesignModuleDetails> detailsList = designModuleDetailsService.list(serviceQueryWrapper);

        //获取dto里面的工序物料明细的数据
        List<ManufactureDesignProcedureModule> modules = dto.getModules();
        //循环删除工序物料明细数据
        for (int i=0;i<modules.size();i++){
            for (int j=0;j<detailsList.size();j++){
                //判断物料明细表的产品编号和工序物料明细的产品编号是否相等
                if(detailsList.get(j).getProductId().equals(modules.get(i).getProductId())){
                    //修改可用数量
                    detailsList.get(j).setResidualAmount(detailsList.get(j).getResidualAmount().add(modules.get(i).getAmount()));
                    designModuleDetailsService.updateById(detailsList.get(j));
                }
            }
            //根据条件删除工序物料明细
            QueryWrapper<ManufactureDesignProcedureModule> moduleQueryWrapper = new QueryWrapper<>();
            moduleQueryWrapper.eq("PARENT_ID",modules.get(i).getParentId());
            manufactureDesignProcedureModuleService.remove(moduleQueryWrapper);
        }

        //修改工序明细的当前工序物料标志 DESIGN_MODULE_TAG D002-1: 已设计
        procedureDetails.setDesignModuleTag("D002-0");
        //修改工序明细里面的物料成本
        procedureDetails.setModuleSubtotal(new BigDecimal(0));

        //修改工序明细表
        boolean flag = manufactureDesignProcedureDetailsService.updateById(procedureDetails);

        return flag;
    }

    @Override
    @Transactional
    public boolean removeProcedure(ManufactureDesignProcedure procedure) {
        Integer id = procedure.getId();
        QueryWrapper<ManufactureDesignProcedureDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<ManufactureDesignProcedureDetails> details = manufactureDesignProcedureDetailsService.list(wrapper);
        QueryWrapper<DesignModule> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.eq("PRODUCT_ID",procedure.getProductId());
        DesignModule designModule = designModuleService.getOne(moduleQueryWrapper);
        QueryWrapper<DesignModuleDetails> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("PARENT_ID",designModule.getId());
        List<DesignModuleDetails> moduleDetails = designModuleDetailsService.list(detailsQueryWrapper);

        for (int i=0;i<details.size();i++){
            //设置每道物料成本
            details.get(i).setModuleSubtotal(new BigDecimal(0));
            //设置每道工序标志位
            details.get(i).setDesignModuleTag("D002-0");
            QueryWrapper<ManufactureDesignProcedureModule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PARENT_ID",details.get(i).getId());
            List<ManufactureDesignProcedureModule> moduleList = manufactureDesignProcedureModuleService.list(queryWrapper);
            for (int j=0;j<moduleList.size();j++){
                for (int x=0;x<moduleDetails.size();x++){
                    //判断物料明细表的产品编号和工序物料明细的产品编号是否相等
                    if(moduleDetails.get(x).getProductId().equals(moduleList.get(j).getProductId())){
                        //修改可用数量
                        moduleDetails.get(x).setResidualAmount(moduleDetails.get(x).getResidualAmount().add(moduleList.get(j).getAmount()));
                        designModuleDetailsService.updateById(moduleDetails.get(x));
                    }
                }
            }
            manufactureDesignProcedureDetailsService.updateById(details.get(i));
            //根据条件删除工序物料明细
            manufactureDesignProcedureModuleService.remove(queryWrapper);
        }
        procedure.setModuleCostPriceSum(new BigDecimal(0));
        boolean flag = manufactureDesignProcedureService.updateById(procedure);
        return flag;
    }


}
